from PyQt4 import QtGui, QtCore
import sys

from ui_chronometre import Ui_Chronometre

class Chronometre(QtGui.QDialog):
	def __init__(self, parent = None):
		QtGui.QDialog.__init__(self, parent)
		self._ui = Ui_Chronometre()
		self._ui.setupUi(self)
		self.nbRow = 0
		self.rows = []
		self.connect(self._ui.addButton, QtCore.SIGNAL("clicked()"), self.addRow)
		#self.setLayout(self._ui.gridLayout)
		#self.layout().addItem(self._ui.gridLayout)
		#print self.layout()
		#print self._ui.gridLayout

	def addRow(self):
		textLabel = QtGui.QLabel("IC " + `self.nbRow+1` + " :")
		removeButton = QtGui.QPushButton("Remove")
		icSpinBox = QtGui.QSpinBox()
		icSpinBox.setMinimum(2)
		icSpinBox.setMaximum(99999999)
		icSpinBox.setValue(10+self.nbRow)
		dureeSpinBox = QtGui.QSpinBox()
		dureeSpinBox.setMinimum(-1)
		dureeSpinBox.setMaximum(99999999)
		dureeSpinBox.setValue(-1)
		startSpinBox = QtGui.QSpinBox()
		startSpinBox.setMinimum(-1)
		startSpinBox.setMaximum(99999999)
		startSpinBox.setValue(-1)
		idRowLayout = self.nbRow + 2
		self._ui.gridLayout.addWidget(textLabel, idRowLayout, 0, 1, 1)
		self._ui.gridLayout.addWidget(icSpinBox, idRowLayout, 1, 1, 1)
		self._ui.gridLayout.addWidget(dureeSpinBox, idRowLayout, 2, 1, 1)
		self._ui.gridLayout.addWidget(startSpinBox, idRowLayout, 3, 1, 1)
		self._ui.gridLayout.addWidget(removeButton, idRowLayout, 4, 1, 1)
		idRowToDelete = self.nbRow
		def tmpFunction():
			print int(str(textLabel.text())[3])-1
			self.removeRow(int(str(textLabel.text())[3])-1)
		nomFunction = "remove" + `idRowToDelete`
		setattr(self, nomFunction, tmpFunction)
		f = getattr(self, nomFunction)
		#self.connect(removeButton, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("removeRow(" + `self.nbRow` + ")"))
		self.connect(removeButton, QtCore.SIGNAL("clicked()"), f)

		self.rows.append([textLabel, icSpinBox, dureeSpinBox, startSpinBox, removeButton])
		self.nbRow += 1
		self._ui.gridLayout.removeWidget(self._ui.lancerButton)
		self._ui.gridLayout.removeWidget(self._ui.arreterButton)
		self._ui.gridLayout.removeWidget(self._ui.addButton)
		self._ui.gridLayout.addWidget(self._ui.lancerButton, self.nbRow+3, 3, 1, 1)
		self._ui.gridLayout.addWidget(self._ui.arreterButton, self.nbRow+3, 4, 1, 1)
		self._ui.gridLayout.addWidget(self._ui.addButton, self.nbRow+2, 4, 1, 1)

	def removeRow(self, idRow): #TODO je m'arrete la : il faudrait que je trouve pourquoi ca marche pas
		print "removeRow called"
		itemsToDelete = []
		for j in xrange(5):
			self.rows[idRow][j].hide()
			#item = self._ui.gridLayout.itemAtPosition(idRow+1, j)
			#widget = item.widget()
			#self._ui.gridLayout.removeItem(item)
			#self._ui.gridLayout.removeWidget(widget)
			#del widget
			#del item
		for i in xrange(idRow, self.nbRow-1):
			#print "i :", i
			self.rows[i+1][0].setText("IC " + `i+1` + " :")
			for j in xrange(5):
				#print self.rows[i+1][j]
				self._ui.gridLayout.addWidget(self.rows[i+1][j], i+2, j, 1, 1)
		self.nbRow -= 1
		for j in xrange(5):
			del self.rows[idRow][0]
		del self.rows[idRow]

		self._ui.gridLayout.removeWidget(self._ui.addButton)
		self._ui.gridLayout.removeWidget(self._ui.lancerButton)
		self._ui.gridLayout.removeWidget(self._ui.arreterButton)
		self._ui.gridLayout.addWidget(self._ui.addButton, self.nbRow+2, 4, 1, 1)
		self._ui.gridLayout.addWidget(self._ui.lancerButton, self.nbRow+3, 3, 1, 1)
		self._ui.gridLayout.addWidget(self._ui.arreterButton, self.nbRow+3, 4, 1, 1)
		self.reSize()

	def reSize(self):
		xSize = self.width()
		ySize = self.height()-40
		print "self.height() :", self.height()
		print "ySize :", ySize
		#self.setGeometry(self.x(), self.y()+40, self.width(), self.height()-40)
		print "Size :", self.size(), "self.height()-40 :", self.height()-40
		self.resize(self.width(), ySize)
		self.window().resize(self.width(), ySize)
		print "Size :", self.size()

if __name__ == '__main__':
	app = QtGui.QApplication(sys.argv)
	chronometre = Chronometre()
	chronometre.addRow()
	#chronometre.removeRow(1)
	chronometre.show()
	sys.exit(app.exec_())

